﻿using Direct.IDAL.User.Jurisdiction;
using DirectSell.Model;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Jurisdiction
{
    /// <summary>
    /// 用户激活数据访问层
    /// </summary>
    public class ActivationRepository:AbsRepository, IActivationRepository
    {
        /// <summary>
        /// 查询待激活会员
        /// </summary>
        /// <param name="UserCode">会员编号</param>
        /// <returns></returns>
        public async Task<PagedList<UserInforMationDetails>> NotActive(string UserCode, int PageIndex, int PageSize)
        {
            PageParam model = new PageParam()
            {
                Fields = "*",
                OrderBy = "UserStatus ASC",
                StrWhere = $"RefereeCode='{UserCode}'",
                TableName = "UserInforMations",
                PageIndex = PageIndex,
                PageSize = PageSize
            };
            var result = await base.PageQuery<UserInforMationDetails>(model);
            return result;
            //StringBuilder sb = new StringBuilder();
            //sb.Append(" SELECT * FROM UserInforMations ");
            //sb.AppendFormat(" WHERE  RefereeCode='{0}' ", UserCode);
            //sb.Append("ORDER BY UserStatus ASC ");
            //return await base.QueryAsync<UserInforMationDetails>(sb.ToString()) as IList<UserInforMationDetails>;
        }
        /// <summary>
        /// 激活会员
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public  void Active(int UserId, ref Hashtable hsTable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations  ");
            sb.AppendFormat(" SET UserStatus=1 WHERE UserStatus=0 AND UserId='{0}' ", UserId);
            hsTable.Add(sb.ToString(), null);
        }
        /// <summary>
        /// 会员删除
        /// </summary>
        /// <param name="UserCode"></param>
        /// <returns></returns>
        public bool Delete(int UserId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" DELETE FROM UserInforMations  ");
            sb.AppendFormat("  WHERE UserStatus=0 AND UserId='{0}' ", UserId);
            return base.Execute(sb.ToString());
        }
        /// <summary>
        /// 重复激活拦截
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public async  Task<bool> CheckStatus(int userid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT UserStatus FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserId= {0} ", userid);
            var result =  await base.QueryAsync<UserInforMationDetails>(sb.ToString());
            var info = result.FirstOrDefault();
            return info.UserStatus == 0;
        }
        /// <summary>
        /// 查会员余额
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public decimal GetBalance(int userid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT VirtualMoney FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserId={0} ", userid);
            return base.QueryFirstT<decimal>(sb.ToString());
        }
        /// <summary>
        /// 查激活所需金额
        /// </summary>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public decimal GetNeedMoney(int userid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT Investment FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserId={0}", userid);
            return base.QueryFirstT<decimal>(sb.ToString());
        }

    }
}
